using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using TnPlus.Entities.Models;
using TnPlus.Entities.Utils;
using TnPlus.Common;

namespace TnPlus.DAL.Base
{
	public class DebtHistoryDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public DebtHistoryDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the DebtHistory table.
		/// </summary>
		public virtual void Insert(DebtHistoryInfo debtHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdDebt", debtHistoryInfo.IdDebt),
				new SqlParameter("@PostDate", debtHistoryInfo.PostDate),
				new SqlParameter("@IdCommon", debtHistoryInfo.IdCommon),
				new SqlParameter("@AccountNo", debtHistoryInfo.AccountNo),
				new SqlParameter("@Debt", debtHistoryInfo.Debt)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "DebtHistory_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the DebtHistory table.
		/// </summary>
		public virtual void Update(DebtHistoryInfo debtHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdDebt", debtHistoryInfo.IdDebt),
				new SqlParameter("@PostDate", debtHistoryInfo.PostDate),
				new SqlParameter("@IdCommon", debtHistoryInfo.IdCommon),
				new SqlParameter("@AccountNo", debtHistoryInfo.AccountNo),
				new SqlParameter("@Debt", debtHistoryInfo.Debt)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "DebtHistory_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the DebtHistory table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idDebt)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdDebt", idDebt)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "DebtHistory_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the DebtHistory table.
		/// </summary>
		public virtual DebtHistoryInfo Select(Guid? idDebt)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdDebt", idDebt)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "DebtHistory_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeDebtHistoryInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the DebtHistory table.
		/// </summary>
		public List<DebtHistoryInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "DebtHistory_SelectAll"))
			{
				List<DebtHistoryInfo> debtHistoryInfoList = new List<DebtHistoryInfo>();
				while (dataReader.Read())
				{
					debtHistoryInfoList.Add(MakeDebtHistoryInfo(dataReader));
				}

				return debtHistoryInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the DebtHistory class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual DebtHistoryInfo MakeDebtHistoryInfo(SqlDataReader dataReader)
		{
			DebtHistoryInfo debtHistoryInfo = new DebtHistoryInfo();
			debtHistoryInfo.IdDebt = SqlClientUtility.GetGuid(dataReader, DbConstants.DEBTHISTORY.IDDEBT);
			debtHistoryInfo.PostDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.DEBTHISTORY.POSTDATE);
			debtHistoryInfo.IdCommon = SqlClientUtility.GetString(dataReader, DbConstants.DEBTHISTORY.IDCOMMON);
			debtHistoryInfo.AccountNo = SqlClientUtility.GetString(dataReader, DbConstants.DEBTHISTORY.ACCOUNTNO);
			debtHistoryInfo.Debt = SqlClientUtility.GetDecimal(dataReader, DbConstants.DEBTHISTORY.DEBT);

			return debtHistoryInfo;
		}

		#endregion
	}
}
